<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title></title>
		<!--<link href="../js/bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet">-->
		<link rel="stylesheet" type="text/css" href="../../../js/bootstrap-3.3.7-dist/css/bootstrap.min.css" />
		<!--<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" />-->
		<link rel="stylesheet" type="text/css" href="../../../css/footer.css" />
		<link rel="stylesheet" type="text/css" href="../../../css/foods_detail.css" />
		<script src="../../../js/flexible.min.js" type="text/javascript" charset="utf-8"></script>
	</head>

	<body>
		<div id="wrap">
			<div id="main" style="padding-top: 1.3rem;">
				<div id="header">
					<a id="headImg" href="javascript:history.go(-1)"><img class="headImg" src="../../../images/12.png" /></a>
					<p id="headTitle">商品详情</p>
				</div>
				<div id="content">
					<div class="foods_detail_wap">
						<div class="foods_detail_pic">
							<img src="" />
						</div>
						<div class="foods_detail_words">
							<div class="foods_detail_words_l">
								<img src="" />
							</div>
							<div class="foods_detail_words_r">
								<h3 id="shopName"></h3>
								<p>金币:&nbsp;&nbsp;<span id="shopPrice"></span> </p>
								<p>数量&nbsp;:&nbsp;<span id="jianNum"></span><label id="shopNum">01</label><span id="addNum"></span></p>
								<p>邮费&nbsp;:&nbsp;包邮</p>

							</div>
						</div>
						<div class="explain">
							<div class="xian" style="display: none;">
								<h3>图文说明</h3>
							</div>
							<ul class="explain_pic" style="display: none;">
								<li id="shopPicinfo">
									<img src="" />
								</li>
								<!--<li>
									<img src="../../../images/coalltion/mianbao.jpg"/>
								</li>-->
							</ul>
							<a id="dianji" href="#">金币兑换</a>
						</div>
					</div>
				</div>
			</div>
		</div>
		<div class="duihuan_box">
			<div class="duihuan">
				<div class="duihuan_top">
					商品兑换？
				</div>
				<div class="duihuan_down">
					<a id="Besure" data-toggle="modal" style="cursor: pointer;" data-target="#myModal">确定</a>
					<a href="#">取消</a>
				</div>
			</div>
		</div>
		<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
			<div class="modal-dialog">
				<div class="modal-content">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×
				</button>
						<h4 class="modal-title" id="myModalLabel">
					编辑收货地址
				</h4>
					</div>
					<div class="modal-body">
						<input type="text" class="form-control" name="" id="addName" style="margin-bottom: 0.2rem;" placeholder="请输入联系人姓名" value="" />
						<input type="text" class="form-control" name="" id="phone" style="margin-bottom: 0.2rem;" placeholder="请输入联系人电话" value="" />
						<input type="text" class="form-control" name="" id="address" style="margin-bottom: 0.2rem;" placeholder="请输入详细地址" value="" />

					</div>
					<div class="modal-footer">
						<button type="button" class="btn btn-default" id="quxiao" data-dismiss="modal">关闭
				</button>
						<button type="button" id="qieding" class="btn btn-danger">
					确定
				</button>
					</div>
				</div>
				<!-- /.modal-content -->
			</div>
			<!-- /.modal-dialog -->
		</div>
		<!-- /.modal -->
	</body>
	<script src="../../../js/jquery-2.2.3.min.js" type="text/javascript" charset="utf-8"></script>
	<script src="../../../js/bootstrap-3.3.7-dist/js/bootstrap.min.js" type="text/javascript" charset="utf-8"></script>
	<script src="../../../js/jquery.cookie.js" type="text/javascript" charset="utf-8"></script>
	<script type="text/javascript">
		function UrlSearch() {
			var name, value;
			var str = location.href; //取得整个地址栏
			var num = str.indexOf("?")
			str = str.substr(num + 1); //取得所有参数   stringvar.substr(start [, length ]
			var arr = str.split("&"); //各个参数放到数组里
			for(var i = 0; i < arr.length; i++) {
				num = arr[i].indexOf("=");
				if(num > 0) {
					name = arr[i].substring(0, num);
					value = arr[i].substr(num + 1);
					this[name] = value;
				}
			}
		}
		var memberId;
		var add;
		var userid;
		$(function() {
			$.ajax({
				type: "get",
				url: "http://47.104.13.109/member-service/auth/current?token=" + $.cookie("tokenRh"),
				async: true,
				success: function(data) {
					memberId = data.id;
				}
			});

		})
		var Request = new UrlSearch();
		console.log(Request);
		var kaiguan = false;
		if(typeof(Request.sid) != 'undefined') {
			$.ajax({
				type: "get",
				url: "http://47.104.13.109/cart-service/selectdetias?cartId=" + Request.sid,
				async: true,
				success: function(data) {
					console.log(data);
					if(data.length != 0) {
						$('#shopName').html(data[0].goodsName);
						$('#shopNum').html(data[0].goodsCount)
						$('#shopPrice').html(data[0].goodsgold);
						$('#shopPicinfo').html('<img src="' + data[0].goodsIntro + '"/>');
						$('.foods_detail_pic').html('<img src="' + data[0].goodsPic + '"/>')
						userid = data[0].userId;
					} else {
						kaiguan = true;
						alert("暂无此商家数据")
					}
				}
			});
		}
		//改变数量
		$("#addNum").click(function() {
			$.ajax({
				type: "get",
				url: "http://47.104.13.109/cart-service/selecdetail?goodsId=" + Request.sid,
				async: true,
				success: function(data) {
					console.log(data);
					$('#shopPrice').html(data[0].goodsgold * $('#shopNum').html());

				}
			})
		})

		$("#jianNum").click(function() {
				if($('#shopNum').html() == 0) {
					$('#shopNum').html('01')
				}
				$.ajax({
					type: "get",
					url: "http://47.104.13.109/cart-service/selecdetail?goodsId=" + Request.sid,
					async: true,
					success: function(data) {
						console.log(data);
						$('#shopPrice').html(data[0].goodsgold * $('#shopNum').html());

					}
				})
			})
			//取消;
		$("#quxiao").on('click', function() {
				$("#qieding").bind('click');
			})
			//确定
		$("#qieding").on("click", function() {
			//登陆判断
			var tokenReztApp = $.cookie("tokenReztApp");
			if(!tokenReztApp) {
				window.location.href = "../../login/login/login2.html";
			}
			//正则验证
			var zhengzphoone = /^1[3|4|5|7|8][0-9]{9}$/;
			if($("#addName").val() == '') {
				return alert("请输入联系人姓名");
			}
			if($("#phone").val() == '') {
				return alert("请输入联系电话");
			}
			if(!zhengzphoone.test($("#phone").val())) {
				return alert("请输入正确的手机号码")
			}
			if($("#address").val() == '') {
				return alert("请输入收货地址");
			}

			$("#qieding").unbind('click');

			$.ajax({
				type: "get",
				url: "http://47.104.13.109/member-service/auth/current?token=" + $.cookie("tokenRh"),
				async: true,
				success: function(res) {
					console.log(res)
					for(var i = 0; i < res.roleList.length; i++){
						if(res.roleList[i].name == "粉丝"){
						    var part = "粉丝";
						}else if(res.roleList[i].name == "会员"){
							var part = "会员";
						}else if(res.roleList[i].name == "创客"){
							var part = "创客";
						}
						
					}
						// 判断会员
						//	if(true) {
					var check = false;
					$.each(res.roleList, function(idx, item) {
						if(item.id == 'f1' || item.id == 'f2') {
							check = true;
						}
					});
					if(check) {
						// 查询商品信息	
						$.ajax({
							type: "get",
							url: "http://47.104.13.109/cart-service/selectdetias?cartId=" + Request.sid,
							async: true,
							success: function(data) {
								console.log(data);
								console.log(data["0"].goodsgold)
								$("#shopNum").change(function() {
										$("#shopPrice").html(data["0"].goodsgold * $("#shopNum").html())
									})
									// 判断金币是否够
								$.ajax({
									type: "get",
									url: "http://47.104.13.109/gold-service/tfGold/" + memberId,
									async: true,
									success: function(res5) {
										console.log(res5)
											//										if(true) {
										if(res5.goldNum >= $("#shopPrice").html()) {
											$("#Besure").unbind("click") //点击事件失效
												//创建购物车
											console.log(res.id)
											
											$.ajax({
												type:"get",
												url:"http://47.104.13.109:80/cart-service/ordersn",
												async:true,
												success:function(orderSn){
													console.log(orderSn)
												
											$.ajax({
												type: "post",
												url: "http://47.104.13.109:80/cart-service/goldadd",
												async: true,
												data: {
													userid: res.id,
													userName: res.loginName,
													usersellid: data["0"].userId,
													usersellName: data["0"].userName,
													goodsId: data["0"].goodsId,
													goodsNum: data["0"].goodsNum,
													goodsName: data["0"].goodsName,
													goodsGold: data["0"].goodsgold,
													goodsCatid: data["0"].goodsCatid,
													goodsPic: data["0"].goodsPic,
													goodsIntro: data["0"].goodsIntro,
													goodsCount: data["0"].goodsCount,
													integralId: data["0"].cartId,
													goodsBuyBack : '0',
													orderSn : orderSn,
													cartStatus: '0',
												},
												success: function(res6) {
													console.log(res6)
													//根据订单编号查询购物车id
													$.ajax({
														type:"get",
														url:"http://47.104.13.109:80/cart-service/goldcart",
														async:true,
														data:{
															userid:res.id
														},
														success:function(resC){
														console.log(resC)
														var cartId;
													$.each(resC, function(idx, item) {
														if (resC.length - 1 == idx) {
															cartId = item.cartId;
														}
														
													});
													//生成金币订单
													$.ajax({
														type: "post",
														url: "http://47.104.13.109/order-service/addgold",
														async: true,
														data: {
															usersellId:data["0"].userId,
															usersellName:data["0"].userName,
															goodsId: data["0"].goodsId,
															goodsName: data["0"].goodsName,
															goosGold: data["0"].goodsgold,
															goodsCatid: data["0"].goodsCatid,
															goodsPic: data["0"].goodsPic,
															goodsIntro: data["0"].goodsIntro,
															goodsCount: data["0"].goodsCount,
															userId: res.id,
															userAccount: res.loginName,
															userIdentity:part,
															userPhone: res.tel,
															orderStatus: "0",
															userAddress: $("#address").val(), //这里缺少输入联系人和手机号码
															userPhone: $("#phone").val(),
															userName: $("#addName").val(),
															orderSn : orderSn,
															cartId: cartId
														},
														success: function(res1) {
															console.log(res1)
															$.ajax({
																type: "put",
																url: "http://47.104.13.109/information-service/information/send",
																async: true,
																data: {
																	inforSendperson: '系统管理员',
																	inforContent: '您在金币商城成功兑换商品（' + data["0"].goodsName + '）, 消费金币：' + $('#shopPrice').html() + '祝你购物愉快~~',
																	inforReceiveperson: $.cookie("memberId"),
																},
																success: function(res3) {
																	console.log(res3);
																	alert("发货成功")
																	history.back();
																	$("#qieding").attr('disabled', false);
																	return false;
																},
																error: function(error) {
																	$("#qieding").attr('disabled', false);
																	alert("发送消息时出现错误，请联系系统管理员")
																}

															});

														},
														error: function(error) {
															$("#qieding").attr('disabled', false);
															alert("生成订单时出现错误，请联系系统管理员")
														}
													});
													},
														errpr:function(error){
															alert("查询购物车id时出现错误，清联系管理员");
														}
													});
												},
												error: function(error) {
													$("#qieding").attr('disabled', false);
													alert("创建购物车时出现错误，请联系系统管理员")
												}
											});
											},
												error:function(error){
													alert("生成订单号时出现错误")
												}
											});
										} else {
											alert("您的金币不足");
											$("#qieding").attr('disabled', false);
										}
									},
									error: function(error) {
										$("#qieding").attr('disabled', false);
										alert("出现错误，请联系系统管理员")
									}
								});
							},
							error: function(error) {
								$("#qieding").attr('disabled', false);
								alert("出现错误，请联系系统管理员")
							}
						});
					} else {
						alert("您还不是会员,需要进行认证")
						window.location.href = "../../personage/person_MakerApplyVip.html";
					}
				},
				error: function(error) {
					$("#qieding").attr('disabled', false);
					alert("出现错误，请联系系统管理员")
				}
			});

		})
	</script>
	<script type="text/javascript">
		$(function() {
	$('.explain a').on('touchend', function() {
					//登陆判断
			var tokenReztApp = $.cookie("tokenReztApp");
			if(!tokenReztApp) {
				window.location.href = "../../login/login/login2.html";
			}
			if (userid == memberId) {
				alert("这是您的寄卖商品哟，拒绝购买");
				return
			}
		$('.duihuan_box').css("display", "block");

	});
	$('.duihuan_down a:first').on('touchend', function() {

	});
	$('.duihuan_down a:last').on('touchend', function() {
		$('.duihuan_box').css("display", "none");
	});
	$('.foods_detail_words_r span:nth-child(1)').on('touchend',function(){
		var num=parseInt($('.foods_detail_words_r label').html());
		if(num>0){
			num--;
		}
		if(num<10){
			num="0"+num;
		}
		$('.foods_detail_words_r label').html(num);
	});
	$('.foods_detail_words_r span:nth-child(3)').on('touchend',function(){
		var num=parseInt($('.foods_detail_words_r label').html());
		if(num<100){
			num++;
		}
		if(num<10){
			num="0"+num;
		}
		$('.foods_detail_words_r label').html(num);
	})
})
	</script>
</html>